const swaggerJSDoc = require('swagger-jsdoc');
const swaggerUi = require('swagger-ui-express');

// Swagger 配置选项
const options = {
  definition: {
    openapi: '3.0.0',
    info: {
      title: '布丁记账 API 文档',
      version: '1.0.0',
      description: '布丁记账应用的 Node.js 后端 API 文档',
    },
    servers: [
      {
        url: 'http://localhost:3000',
        description: '本地开发服务器',
      },
    ],
    components: {
      securitySchemes: {
        bearerAuth: {
          type: 'http',
          scheme: 'bearer',
          bearerFormat: 'JWT',
        },
      },
      schemas: {
        // 通用响应模型
        ApiResponse: {
          type: 'object',
          properties: {
            code: {
              type: 'string',
              description: '响应码',
            },
            message: {
              type: 'string',
              description: '响应消息',
            },
            data: {
              type: 'object',
              description: '响应数据',
              nullable: true,
            },
          },
        },
        // 用户模型
        User: {
          type: 'object',
          properties: {
            id: {
              type: 'integer',
              description: '用户ID',
            },
            username: {
              type: 'string',
              description: '用户名',
            },
            email: {
              type: 'string',
              description: '邮箱',
              nullable: true,
            },
            avatar: {
              type: 'string',
              description: '头像',
              nullable: true,
            },
            create_time: {
              type: 'string',
              format: 'date-time',
              description: '创建时间',
            },
          },
        },
        // 账单模型
        Bill: {
          type: 'object',
          properties: {
            id: {
              type: 'integer',
              description: '账单ID',
            },
            user_id: {
              type: 'integer',
              description: '用户ID',
            },
            category_id: {
              type: 'integer',
              description: '分类ID',
            },
            amount: {
              type: 'number',
              format: 'double',
              description: '金额',
            },
            type: {
              type: 'string',
              description: '类型: income-收入，expense-支出',
              enum: ['income', 'expense'],
            },
            remark: {
              type: 'string',
              description: '备注',
              nullable: true,
            },
            bill_date: {
              type: 'string',
              format: 'date-time',
              description: '账单日期',
            },
            create_time: {
              type: 'string',
              format: 'date-time',
              description: '创建时间',
            },
          },
        },
        // 分类模型
        Category: {
          type: 'object',
          properties: {
            id: {
              type: 'integer',
              description: '分类ID',
            },
            name: {
              type: 'string',
              description: '分类名称',
            },
            icon: {
              type: 'string',
              description: '图标',
            },
            bg_class: {
              type: 'string',
              description: '背景颜色类',
            },
            create_time: {
              type: 'string',
              format: 'date-time',
              description: '创建时间',
            },
          },
        },
      },
    },
  },
  // 指定API文档注释的位置
  apis: ['./src/routes/*.js', './src/controllers/*.js'],
};

// 初始化Swagger规范
const swaggerSpec = swaggerJSDoc(options);

// 导出Swagger配置
module.exports = {
  swaggerUi,
  swaggerSpec,
};